<?php
/**
 * Nweb Editor
 *
 * This script is protected by copyright. Its use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.editor.app
 * @subpackage  controller
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     1.0 $Id$
 * @link        https://nweb-editor.googlecode.com
 * @link        http://editor.nweb.pl
 */

namespace pl\nweb\editor\app\controller;

/**
 * Kontroler błędów
 *
 * @category    source
 * @package     pl.nweb.editor.app
 * @subpackage  controller
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2012 Krzysztof Kardasz
 * @version     1.0 $Id$
 */
class Error extends Controller
{
    /**
     * Not found
     */
    public function indexAction()
    {
        $this->_forward('error');
    }

    /**
     * Not found
     */
    public function notfoundAction()
    {
        $this->getResponse()->setHttpResponseCode(404);
    }

    /**
     * Unauthorized
     */
    public function unauthorizedAction()
    {
        $this->getResponse()->setHttpResponseCode(401);
    }

    /**
     * Forbidden
     */
    public function forbiddenAction()
    {
        $this->getResponse()->setHttpResponseCode(403);
    }

    /**
     * Obsługa błędów
     */
    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');

        if (!$errors) {
            $this->view->message = 'You have reached the error page';
            return;
        }

        switch ($errors->type) {
            case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
            case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
                $this->getResponse()->setHttpResponseCode(404);
                $this->_forward('notfound');
                break;
            default:
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = 'Application error';
                break;
        }

        $this->_log($errors->exception->getMessage(), \Zend_Log::ERR);
        error_log($errors->exception->getMessage());

        if ($this->getInvokeArg('displayExceptions') == true) {
            $this->view->exception = $errors->exception;
        }

        $this->view->request   = $errors->request;
    }
}